package com.niannian;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

import com.niannian.db.MyDBUser;
import com.niannian.db.DatabaseApi;
import com.niannian.service.NotificationService;
import com.niannian.util.Common;
import com.niannian.util.UserInfoManager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * activity的基类
 * 
 * @author CcCc *
 */

public class BaseActivity extends Activity {

	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	protected DatabaseApi databaseapi = DatabaseApi.getDataBaseApi(this);
	private static Map<String, Set<Activity>> activityGroup = new HashMap<String, Set<Activity>>();
	private static Map<String, Activity> activitys = new HashMap<String, Activity>();
	public static final String Logined_Group = "logined";
	public static final String Register_Group = "register";
	protected MyApplication myApplication = MyApplication.getInstance();
	public static int mScreenWidth;
	public static int mScreenHeight;
	protected static BitmapDrawable drawable = null;
	public static String USER_HEAD_IMAGE_DEFAULT = "headview.jpg";

	/**
	 * 退出时间
	 */
	private long mExitTime;
	protected static Bitmap Loadfail;
	/**
	 * 退出间隔
	 */
	public static final int INTERVAL = 2000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Common.CheckNetworkState(BaseActivity.this, true);
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		mScreenWidth = metric.widthPixels;
		mScreenHeight = metric.heightPixels;
	}

	public JSONObject checkReturnData(String jsonString) {
		return Common.checkReturnData(jsonString, this);
	}

	public JSONObject checkReturnData(String jsonString, boolean isValidate) {
		return Common.checkReturnData(jsonString, this, isValidate);
	}

	public synchronized static void addActToGroup(String Groupname, Activity cur) {
		Set<Activity> Group = activityGroup.get(Groupname);
		if (Group == null) {
			Group = new HashSet<Activity>();
			activityGroup.put(Groupname, Group);
		}
		Group.add(cur);
	}

	/*
	 * public synchronized static void addActToMap(String activityname, Activity
	 * cur) { activitys.put(activityname, cur); }
	 */

	public synchronized static void destroyGroup(String Groupname) {
		Set<Activity> group = activityGroup.get(Groupname);
		if (group != null) {
			for (Activity one : group) {
				if (one != null) {
					one.finish();
				}
			}

		}

	}

	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	protected void openActivity(Class<?> pClass, int type) {
		openActivity(pClass, null, type);
	}

	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this, pClass);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	protected void openActivity(Class<?> pClass, Bundle pBundle, int type) {
		Intent intent = new Intent(this, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(intent, type);
	}

	protected void openActivity(String pAction) {
		openActivity(pAction, null, 0000);
	}

	protected void openActivity(String pAction, int type) {
		openActivity(pAction, null, type);
	}

	protected void openActivity(String pAction, Bundle pBundle, int type) {
		Intent intent = new Intent(pAction);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		if (type != 0000) {
			startActivityForResult(intent, type);
		} else {
			startActivity(intent);
		}

	}

	protected void myfinish() {
		finish();
		overridePendingTransition(R.anim.push_right_in2, R.anim.push_right_to2);
	}

	protected void startAnimationLeftToRight() {
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

	protected void startAnimationBottomToTop() {
		overridePendingTransition(R.anim.roll_up, R.anim.roll);
	}

	/**
	 * 判断两次返回时间间隔,小于两秒则退出程序
	 */
	protected void exit() {
		if (System.currentTimeMillis() - mExitTime > INTERVAL) {
			Common.tip(this, "再按一次  退出程序");
			mExitTime = System.currentTimeMillis();
		} else {
			backHome();
			/*
			 * finish(); stopService();
			 * android.os.Process.killProcess(android.os.Process.myPid());
			 * System.exit(0);
			 */
		}
	}

	public void backHome() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.addCategory(Intent.CATEGORY_HOME);
		startActivity(intent);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {
			finish();
			return true;
		}
		return super.onTouchEvent(event);
	}

	private boolean isOutOfBounds(Activity context, MotionEvent event) {
		final int x = (int) event.getX();
		final int y = (int) event.getY();
		final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
		final View decorView = context.getWindow().getDecorView();
		return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop)) || (y > (decorView.getHeight() + slop));
	}

	public void update_gUser() {
		userInfoManager.updateDBUser();
	}

	private void stopService() {
		Intent i = new Intent(this, NotificationService.class);
		this.stopService(i);
	}

	@Override
	protected void onDestroy() {
		if (Loadfail != null) {
			Loadfail.recycle();
		}
		super.onDestroy();
	}

	@Override
	protected void onResume() {

		JPushInterface.onResume(this);

		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}
}
